<template>
	<view>
		<view class="contents">
			<view class="first-part">
				<image mode="widthFix" :src="show_image"></image>
			</view>
			<view class="activity-box" v-if="buy_plus &&activityList && activityList.length">
				<view class="activity-title">直接购买套餐</view>
				<view class="activity-list-box">
					<block v-for="(item, id) in activityList" :key="id">
						<view :class="'activity-list' + (buyIndex==id?' activity-active':'')" @tap="chooseActivity"
							:data-index="id">
							<view class="activity-name">{{item.time_name}}</view>
							<view class="activity-time">{{item.time}}天</view>
							<view class="activity-price">
								<text>￥</text>
								<text>{{item.price}}</text>
							</view>
						</view>
					</block>

				</view>
			</view>
			<view class="first-find" v-if="buy_goods&&user_type==1">
				<view class="footer-content">
					<view class="plus_shop" @tap="shop_goods">
						<image mode="widthFix" :src="image_header + 'plus_shop.png'">
						</image>
					</view>
				</view>
			</view>
			<view v-if="show_text" class="show_text">
				<jyf-parser :html="article_text"></jyf-parser>
			</view>
		</view>

		<view class="plus_button" v-if="buy_plus || recharge_plus">
			<view v-if="recharge_plus" :class="'choose-btn btnsecond ' + (buy_plus?'btn-r-1':'')" @tap="rechargePlus">
				{{user_type==2?'充值续费':'充值购买'}}
			</view>
			<view v-if="buy_plus" :class="'choose-btn btnfirst ' + (recharge_plus?'btn-r-2':'')" @tap="becomePlus">
				立即{{user_type==2?'续费':'购买'}}</view>
		</view>
		<!-- 输入手机号 -->
		<view class="moblie-phone" v-if="show_eject">
			<view class="phone">
				<view class="cardnum">
					<text>请输入手机号</text>
				</view>
				<view class="section">
					<input type="number" maxlength="11" placeholder-class="pl" placeholder="请输入手机号" :value="phone"
						@input="phoneFun"></input>
				</view>
				<view class="moblie_btn" @tap="placeOrder">确认</view>
				<view @tap.stop="close_eject">
					<icon type="cancel" size="26" color="#ffd100"></icon>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// pages/PlusHome/PlusHome.js
	const App = getApp().globalData;


	export default {
		data() {
			return {
				user_type: 1,
				// 1.普通用户 2.付费会员 3.会员
				show_image: '',
				// 展示图
				show_text: false,
				// 展示内容
				buy_plus: false,
				// 直接购买
				recharge_plus: false,
				// 充值
				buy_goods: false,
				// 购买商品
				show_eject: false,
				// 输入手机号
				phone: '',
				//手机号
				open_type: '',
				//方式 1.直接购买 2.充值购买
				activityList: [],
				//购买或续费列表
				buyIndex: 0 //已选择购买的套餐下标
					,
				canClick: false,
				article_text: "",
				image_header: ''
			};
		},
		async onShow(options) {
			await this.$onLaunched;
			// #ifdef MP-ALIPAY
			if (JSON.stringify(options) == '{}') {
				options = my.getLaunchOptionsSync().query;
			}
			// #endif
			if (options && (options.store_id)) {
				this.$store.store_id = options.store_id;
			}
			this.image_header = this.$store.image_header;
			this.judge_user();
		},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage() {
			return {
				title: 'PLUS会员',
				path: '/pages/mine/plusHome/plusHome?store_id=' + this.$store.store_id +
					'&share_uid=' + App.XcxData.userInfo.uid,
				success: function(res) { // 转发成功
				},
				fail: function(res) { // 转发失败
				}
			};
		},
		methods: {
			chooseActivity: function(e) {
				console.log(e)
				this.setData({
					buyIndex: e.currentTarget.dataset.index
				});
			},
			rechargePlus: function() {
				//充值
				uni.navigateTo({
					url: '../recharge/recharge?recharge_type=2'
				});
			},
			shop_goods: function() {
				uni.navigateTo({
					url: '../../product/plusGoods/plusGoods'
				});
			},
			// 判断用户类型
			judge_user: function() {
				var that = this;
				getApp().globalData.sendRequest({
					url: '/PlusVip/getuser',
					type: "post",
					showLoading: false,
					success: function(res) {
						if (res.status == 1) {
							that.setData({
								user_type: res.user_type,
								show_eject: false
							}); // 获取展示数据
							
							getApp().globalData.XcxData.userInfo.type = res.user_type;
							getApp().globalData.XcxData.userInfo.user_type = res.user_type;
							getApp().globalData.XcxData.userInfo.vid = res.vid;

							that.get_plus_home(res.user_type);
						}
					}
				});
			},
			// 获取展示页面数据
			get_plus_home: function(user_type) {
				// user_type 1.普通用户 2.付费会员 3.会员
				// var url = user_type == 2 ? '/PlusVip/guidance_renew' : '/PlusVip/guidance_become';
				var that = this;
				getApp().globalData.sendRequest({
					url: '/PlusVip/guidance_v1',
					type: "post",
					showLoading: false,
					data: {
						user_type: user_type
					},
					success: function(res) {
						if (res.status == 1) {
							var obj = {
								show_image: res.data.background
							};

							if (res.data.text) {
								//WxParse.wxParse('text', 'html', res.data.text, that, 5)
								setTimeout(() => {
									that.article_text = res.data.text;
								}, 200);
								obj.show_text = true; // 内容
							} else {
								obj.show_text = false;
							}

							if (res.data.buy_plus == 2) {
								obj.buy_plus = true; // 开启直接购买
							} else {
								obj.buy_plus = false;
							}

							if (res.data.recharge_plus == 2) {
								obj.recharge_plus = true; // 会员充值
							} else {
								obj.recharge_plus = false;
							}

							if (res.data.buy_goods && res.data.buy_goods == 2) {
								obj.buy_goods = true; // 购买商品
							} else {
								obj.buy_goods = false;
							}

							if (res.activityList && res.activityList.length) {
								obj.activityList = res.activityList; //直接购买或者续费活动
							} else {
								obj.activityList = [];
							}

							that.setData(obj);
						}
					}
				});
			},
			// 付费会员购买操作
			becomePlus: function() {
				if (this.user_type == 1) {
					// 普通用户 输入手机号
					this.setData({
						show_eject: true
					});
				} else {
					this.placeOrder(); // 下单
				}
			},

			// 手机号输入
			phoneFun(e) {
				this.setData({
					phone: e.detail.value
				});
			},

			// 关闭弹出
			close_eject: function() {
				this.setData({
					show_eject: false,
					phone: ''
				});
			},
			// 购买开通（下单）
			placeOrder: function() {
				if (!this.activityList.length) {
					uni.showToast({
						title: '商家暂未设置可购买套餐',
						icon: 'none',
						duration: 2000
					});
					return;
				}

				if (this.user_type == 1) {
					var phone_reg = /^1(3|4|5|7|8|9)\d{9}$/; // 验证手机号

					if (!phone_reg.test(this.phone)) {
						uni.showToast({
							title: '手机格式不正确',
							icon: 'none',
							duration: 1500
						});
						return;
					}
				}

				var that = this;
				uni.showLoading({
					title: '下单中...',
					// #ifndef MP-ALIPAY
					mask: true,
					// #endif
				});
				getApp().globalData.sendRequest({
					url: '/PlusVip/placeOrder_v1',
					type: "post",
					showLoading: false,
					data: {
						user_type: that.user_type,
						phone: that.phone,
						activity_id: that.activityList[that.buyIndex].id
					},
					success: function(ret) {
						uni.hideLoading();

						if (ret.status == 1) {
							// 下单成功
							// #ifdef MP-WEIXIN
							uni.requestPayment({
								timeStamp: ret.payData.timeStamp,
								nonceStr: ret.payData.nonceStr,
								//字符串随机数
								package: ret.payData.package,
								signType: ret.payData.signType,
								paySign: ret.payData.paySign,
								success: function(re) {
									if (re.errMsg == 'requestPayment:ok') {
										// 下单
										uni.showLoading({
											title: '查询中...',
											mask: true
										});
										var count = 0;
										that.searchOrder(ret.pay_order_id, 0); //查询订单
									} else {
										that.setData({
											canClick: true
										});
									}
								}
							});

							// #endif
							// #ifdef MP-ALIPAY
							my.tradePay({
								tradeNO: ret.payData.trade_no,
								success: function(re) {
									if (re.errMsg == 'requestPayment:ok') {
										// 下单
										uni.showLoading({
											title: '查询中...',
											mask: true
										});
										var count = 0;
										that.searchOrder(ret.pay_order_id, 0); //查询订单
									} else {
										that.setData({
											canClick: true
										});
									}
								}
							});

							// #endif
						} else {
							// 下单失败
							uni.showModal({
								title: '提示',
								content: ret.msg,
								showCancel: false
							});
						}
					}
				});
			},
			searchOrder: function(order_no, count) {
				var that = this;
				getApp().globalData.sendRequest({
					url: '/PlusVip/searchOrder',
					type: "post",
					data: {
						pay_order_id: order_no
					},
					showLoading: false,
					success: function(res) {
						if (res.status == 1) {
							uni.hideLoading();

							if (that.user_type == 1) {
								var msg = '开通成功，密码为手机号后6位，请及时前往个人中心修改';
							} else if (that.user_type == 2) {
								var msg = '续费成功';
							} else {
								var msg = '开通成功';
							}

							uni.showModal({
								title: '提示',
								content: msg,
								showCancel: false,
								success: function(res) {
									if (res.confirm) {
										that.judge_user();
									}
								}
							});
						} else if (res.status == 2) {
							count++;

							if (count > 10) {
								uni.hideLoading();
								uni.showModal({
									title: '提示',
									content: "订单查询失败",
									showCancel: false
								});
							} else {
								setTimeout(function() {
									that.searchOrder(order_no, count);
								}, 1000);
							}
						} else {
							that.setData({
								canClick: true
							});
							uni.hideLoading();
							clearInterval(timer);
							uni.showModal({
								title: '提醒',
								content: res.msg,
								showCancel: false,
								confirmColor: '#fd633a'
							});
						}
					}
				});
			}
		}
	};
</script>
<style>
	@import "./plusHome.css";
</style>
